package com.foreknow.protals.service.impl;

import com.foreknow.protals.dao.PmsProductCategoryAttributeRelationDao;
import com.foreknow.protals.dto.PmsProductCategoryChildrenDto;
import com.foreknow.protals.dto.PmsProductCategoryDto;
import com.foreknow.protals.mbg.mapper.PmsProductCategoryAttributeRelationMapper;
import com.foreknow.protals.mbg.mapper.PmsProductCategoryMapper;
import com.foreknow.protals.mbg.mapper.PmsProductMapper;
import com.foreknow.protals.mbg.model.*;
import com.foreknow.protals.service.PmsProductCategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class PmsProductCategoryServiceImpl implements PmsProductCategoryService {
    @Autowired
    private PmsProductCategoryMapper productCategoryMapper;

    @Autowired
    private PmsProductMapper productMapper;

    @Autowired
    private PmsProductCategoryAttributeRelationMapper productCategoryAttributeRelationMapper;

    @Autowired
    private PmsProductCategoryAttributeRelationDao productCategoryAttributeRelationDao;
    @Override
    public int create(PmsProductCategoryDto productCategoryDto) {
        PmsProductCategory productCategory = new PmsProductCategory();
        BeanUtils.copyProperties(productCategoryDto,productCategory);
        //设置分类
        setCategoryLevel(productCategory);
        int isRight = productCategoryMapper.insertSelective(productCategory);
        //创建筛选属性的关联
        List<Long> list =  productCategoryDto.getProductAttributeIdList();
        if(!CollectionUtils.isEmpty(list)) {
            //批量添加
            insertBathList(productCategory.getId(),list);
        }
        return isRight;
    }

    @Override
    public int update(Long id, PmsProductCategoryDto productCategoryDto) {
        PmsProductCategory productCategory = new PmsProductCategory();
        productCategory.setId(id);
        BeanUtils.copyProperties(productCategoryDto,productCategory);
        setCategoryLevel(productCategory);
        //更新商品分类要更新商品中的名称
        PmsProduct product = new PmsProduct();
        product.setProductCategoryName(productCategory.getName());
        PmsProductExample example = new PmsProductExample();
        example.createCriteria().andProductCategoryIdEqualTo(id);
        //update psm_product set product_category_name=? where product_category_id=?
        productMapper.updateByExampleSelective(product,example);
        //同时更新筛选属性的信息
        if(!CollectionUtils.isEmpty(productCategoryDto.getProductAttributeIdList())) {
            PmsProductCategoryAttributeRelationExample relationExample = new PmsProductCategoryAttributeRelationExample();
            relationExample.createCriteria().andProductCategoryIdEqualTo(id);
            //delete from pms_product_category_attribute_relation where product_category_id=?
            productCategoryAttributeRelationMapper.deleteByExample(relationExample);
            //批量添加
            insertBathList(id,productCategoryDto.getProductAttributeIdList());
        }else {
            PmsProductCategoryAttributeRelationExample relationExample = new PmsProductCategoryAttributeRelationExample();
            relationExample.createCriteria().andProductCategoryIdEqualTo(id);
            productCategoryAttributeRelationMapper.deleteByExample(relationExample);
        }
        //update pms_product_category set parent_id=? ,name=?,level=?........  where id = ?
        return productCategoryMapper.updateByPrimaryKeySelective(productCategory);
    }

    @Override
    public List<PmsProductCategoryChildrenDto> listChildren() {
        return null;
    }

    private void insertBathList(Long id, List<Long> list) {
        List<PmsProductCategoryAttributeRelation> relationList = new ArrayList<>();
        for (Long productAttrId:list) {
            PmsProductCategoryAttributeRelation relation = new PmsProductCategoryAttributeRelation();
            relation.setProductAttributeId(productAttrId);
            relation.setProductCategoryId(id);
            relationList.add(relation);
        }
        productCategoryAttributeRelationDao.insertBathList(relationList);
    }

    private void setCategoryLevel(PmsProductCategory productCategory) {
        if(productCategory.getParentId() == 0) {
            productCategory.setLevel(0);
        }else {
            PmsProductCategory pmsProductCategory = productCategoryMapper.
                    selectByPrimaryKey(productCategory.getParentId());
            if(pmsProductCategory != null) {
                productCategory.setLevel(productCategory.getLevel() + 1);
            }else {
                productCategory.setLevel(0);
            }
        }
    }
}
